package com.cbtx.module_sp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cbtx.module_sp.R;

public class LastTimeConfirmDialog extends Dialog {

    public Context mContext;

    public OnDialogListener onDialogListener;

    Handler handler = new Handler();

    public LastTimeConfirmDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setCancelable(false);

        initView();
    }

    TextView laTime;

    private void initView() {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_comfig_last_time, null);

        laTime = contentView.findViewById(R.id.tv_lastTime);

        TextView tvCancel = contentView.findViewById(R.id.tv_cancel);
        TextView tvAgree = contentView.findViewById(R.id.tv_agree);


        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(null);
                if (onDialogListener != null) {
                    onDialogListener.onCancel();
                    dismiss();
                }
            }
        });

        tvAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacksAndMessages(null);
                if (onDialogListener != null) {
                    onDialogListener.onAgree();
                    dismiss();
                }
            }
        });


        showTitme();
        setCancelable(true);
        setContentView(contentView);
    }


    int time = 30;

    private void showTitme() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                time = time - 1;
                laTime.setText(time+"");

                if (time >= 1) {
                    showTitme();
                }else{
                    onDialogListener.onCancel();
                    dismiss();
                }
            }
        }, 1000);
    }

    public  LastTimeConfirmDialog setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
        return this;
    }


    public interface OnDialogListener {
        void onCancel();

        void onAgree();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {

            return true;

        } else {

            return false; // 默认返回 false

        }

    }
}
